<?php

namespace Admin\Controller;

use Think\Controller;

class MainController extends Controller {
    
    function _initialize(){
        header("Content-type: text/html; charset=utf-8");
        // if(!$this->check()){
        //     $this->returnError("010");
        // }
    }

    /**
     * 返回信息
     * @queyuan
     * 2016/5/30
     * @param $info
     */
    protected function returnInfo($info){
        $info['return_status'] = 1;
        trace($info,'return info','api',true);
        $this->ajaxReturn($info);
    }

    /**
     * 返回错误信息
     * @queyuan
     * 2016/5/30
     * @param $error_code
     */
    protected function returnError($error_code){
        $info['error_code'] = $error_code;
        $info['return_status'] = 0;
        trace($info,'return error','api',true);
        $this->ajaxReturn($info);
    }

    /**
     * 验证参数合法性
     * @queyuan
     * 2016/5/30
     * @return bool
     */
    protected function check(){
        $param = I('param.');
        if (!$param['sign']){
            return false;
        }
        if (!$param['time_stamp'] || !is_numeric($param['time_stamp'])) {
            return false;
        }
        if($param['share'] && ($param['share'] == 'share')){
            unset($param['share']);
            unset($param['from']);
            unset($param['isappinstalled']);
            unset($param['appinstall']);
        }
        $sign_from_post = $param['sign'];
        unset($param['sign']);
        ksort($param);
        $key = md5('kksonegoapiiOSiPhone');
        $sign = implode('',array_values($param)).$key;
        $sign = md5($sign);
        if ($sign == $sign_from_post) {
            return true;
        }
    }
}
